#ifndef WANMESH_BPF_COMMON_H
#define WANMESH_BPF_COMMON_H

// Ethernet protocol types
#define ETH_P_IP 0x0800
#define ETH_P_IPV6 0x86DD

// IP protocol numbers
#define IPPROTO_ICMP 2
#define IPPROTO_IPIP 4   // IPv4-in-IPv6
#define IPPROTO_IPV6 41
#define IPPROTO_ROUTING 43  // IPv6 Routing Header (SRH)
#define IPPROTO_ICMPV6 58

#define AF_INET 2
#define AF_INET6 10

// TC actions
#define TC_ACT_UNSPEC -1
#define TC_ACT_OK 0
#define TC_ACT_SHOT 2
#define TC_ACT_REDIRECT 7

#define IPV6_HDR_LEN 40

#define SRV6_SEGMENT_LIST_MAX_LENGTH 127

#define IPV4_ADDR_SIZE 4
#define IPV6_ADDR_SIZE 16

// IP报文偏移
#define IP_OFFSET_ETHERNET 14
#define IP_OFFSET_RAW_IP 0

// 定义SRv6行为类型
typedef __u16 Srv6Behavior;
const Srv6Behavior SRV6_BEHAVIOR_END = 0x0001;
const Srv6Behavior SRV6_BEHAVIOR_END_DT46 = 0x0014;

// 设备类型
typedef __u32 DeviceType;
const DeviceType DEVICE_TYPE_ETHERNET = 1;
const DeviceType DEVICE_TYPE_RAW_IP = 2;

typedef struct Ipv4Addr8 {
    __u8 addr8[4];
} Ipv4Addr8;

typedef struct Ipv6Addr8{
    __u8 addr8[16];
} Ipv6Addr8;

typedef struct Ipv4LpmKey {
    __u32 prefix_length;
    Ipv4Addr8 address;
} Ipv4LpmKey;

typedef struct Ipv6LpmKey {
    __u32 prefix_length;
    Ipv6Addr8 address;
} Ipv6LpmKey;

typedef struct Srv6Route {
    Ipv6Addr8 segments[SRV6_SEGMENT_LIST_MAX_LENGTH];
} Srv6Route;

typedef struct LocalSrv6SidConfig {
    Srv6Behavior behavior;
} LocalSrv6SidConfig;

typedef struct SegmentRoutingHeader {
    __u8 next_header;
    __u8 hdr_ext_len;
    __u8 routing_type;
    __u8 segments_left;
    __u8 last_entry;
    __u8 flags;
    __be16 tag;
} SegmentRoutingHeader;

bool ipv6_addr8_is_zero(const Ipv6Addr8* addr) {
    const __u64* addr64 = (const __u64*)addr->addr8;
    return (addr64[0] == 0) && (addr64[1] == 0);
}

#endif